<?php

class ShopCategory {
	private $ID;
	private $CategoryName;
	private $CategoryItems = array();
	
	private $Controller;
	private $Limits;
	
	function __construct($CategoryID, $Controller, $Limits = null){
		$this->Controller = $Controller;
		$this->Limits = ($Limits == null) ? null : " ".$Limits;
		
		$MySQL = $this->Controller->UseApps["mysql"];
		
		$SelectCategory = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}shop_categories WHERE id = $CategoryID");
		
		if(mysql_num_rows($SelectCategory) <= 0){
			return false;
		}
		
		$CatFetch = mysql_fetch_assoc($SelectCategory);
		
		$this->ID = $CatFetch["id"];
		$this->CategoryName = $CatFetch["category_name"];
	}
	
	function getID(){
		return $this->ID;
	}
	
	function getName(){
		return $this->CategoryName;
	}
	
	function getItems(){
		if($this->ID == null){
			return false;
		}
			
		if(count($this->CategoryItems) > 0){
			return $this->CategoryItems;
		}
		
		$MySQL = $this->Controller->UseApps["mysql"];
		
		$SelectItems = $MySQL->query("SELECT id FROM {$MySQL->getPrefix()}shop_items WHERE category_id = {$this->ID} ORDER BY id DESC{$this->Limits}");
		
		if(mysql_num_rows($SelectItems) <= 0){
			return false;
		}
		
		while ($item = mysql_fetch_assoc($SelectItems)) {
			$this->CategoryItems[] = new ShopItem($item["id"], $this->Controller);	
		}
		
		return $this->CategoryItems;
	}
	
	function getCatItemsQuantity(){
		$MySQL = $this->Controller->UseApps["mysql"];
		
		$Result = $MySQL->query("SELECT id FROM {$MySQL->getPrefix()}shop_items WHERE category_id = {$this->ID}");
		
		return mysql_num_rows($Result);
	}
	
}

?>